package cn.com.pxto.web.tag;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

import cn.com.pxto.dao.LessonTypeDAO;
import cn.com.pxto.dao.UserDAO;

public class UserNameTag extends BodyTagSupport {
	private String uid;

	private static  UserDAO userDAO;
	
	private static HashMap<String , String> userNameMap;
	
	
	
	public UserNameTag() {
				
	}

	public int doStartTag() throws JspException {
		Boolean userHasNew=(Boolean) pageContext.getServletContext().getAttribute("userHasNew");
		if(userDAO == null ){
			// ��ϵͳ�л�ȡspring����
			ApplicationContext ctx = (XmlWebApplicationContext) pageContext.getServletContext().getAttribute("org.springframework.web.struts.ContextLoaderPlugIn.CONTEXT.");
			userDAO = (UserDAO) ctx.getBean("userDAO");
			userNameMap = userDAO.listUserForTag();
		}else{
			if(userHasNew!= null){
				ApplicationContext ctx = (XmlWebApplicationContext) pageContext.getServletContext().getAttribute("org.springframework.web.struts.ContextLoaderPlugIn.CONTEXT.");
				userDAO = (UserDAO) ctx.getBean("userDAO");
				userNameMap = userDAO.listUserForTag();
				pageContext.getServletContext().removeAttribute("userHasNew");
			}
		}
		String userName = userNameMap.get(uid);
		try {
			if (userName == null) {
				pageContext.getOut().print("-1");
			} else {
				pageContext.getOut().print(userName);
			}
		} catch (IOException e) {
			throw new JspException("Print Error:" + e.getMessage());
		}
		return super.doStartTag();
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

}
